﻿// -----------------------------------------------------------------------
// <copyright file="Snake.cs" company="Microsoft">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace Cspl.AnimalFight.Lib.Data
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Cspl.AnimalFight.Lib.Interfaces;
    using Cspl.AnimalFight.Lib.EventHandlers;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class Snake : AnimalBase, IAttack, IDefend, IAttackable
    {
        #region Field

        #endregion

        #region Event
        public event AttackDelegate AttackEvent;
        public event DefendDelegate DefendEvent;
        #endregion

        #region Property
        public bool IsDefending
        {
            get;
            set;
        }
        #endregion

        #region Method
        public void Attack(string targetName)
        {
            if (this._animalName != targetName)
            {
                if (IsExistOnBattleField("attack") && AttackEvent != null)
                {
                    AttackEventHandler e = new AttackEventHandler
                    {
                        AnimalTargetName = targetName,
                        AP = this.AP
                    };
                    AttackEvent(this, e);
                }
            }
            else
            {
                Console.WriteLine("Cannot attack itself.");
            }
        }

        public void Defend()
        {
            if (IsExistOnBattleField("defend") && DefendEvent != null)
            {
                DefendEvent(this, new DefendEventHandler
                {
                    AnimalName = this.Name,
                    Target = Animals.Snake
                });
            }
        }

        public override void Escape()
        {
            if (IsExistOnBattleField("escape"))
            {
                this.HasEscaped = true;
                Console.Write("The {0} has ", this.Name);
                Console.BackgroundColor = ConsoleColor.Red;
                Console.WriteLine("escaped");
                Console.ResetColor();
            } 
        }

        public void Attacked(int ap)
        {

            if (IsExistOnBattleField("attack"))
            {


                int realDp = this.IsDefending ? this.DP + 10 : this.DP;

                this.HP -= (ap - realDp);
                this.HP = this.HP < 0 ? 0 : this.HP;
                Console.WriteLine("HP is decreased {0}", realDp - ap);

                if (this.HP <= 0)
                {
                    Console.Write("The {0} is ", this.Name);
                    Console.BackgroundColor = ConsoleColor.Red;
                    Console.WriteLine("dead");
                    Console.ResetColor();
                    this.IsDead = true;
                }

                //set isDefending to false when it is true
                if (this.IsDefending)
                {
                    this.IsDefending = false;
                }    
            }
        }
        #endregion

        
    }
}
